package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.mall.lib.domain.SysRolePermission;
import com.rockcent.mall.lib.dto.MerchantShoppingCartDto;
import com.rockcent.mall.lib.dto.ShoppingCartProductDto;
import com.rockcent.mall.lib.dto.SysPermissionDto;
import com.rockcent.mall.lib.dto.SysRolePermissionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * Created by oyhk on 2016/10/12.
 *
 */
@Repository
public class SysRolePermissionRepositoryImpl implements SysRolePermissionRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(SysRolePermissionRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<SysRolePermissionDto> findByRoleIdInAndUserInfoId(List<Long> roleIdList, Long userInfoId) {
        SQLListDoing<SysRolePermissionDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from sys_role_permission srp left join sys_user_role sur on srp.role_id = sur.role_id and srp.mall_id = sur.mall_id where srp.role_id in (:roleIdList) and sur.user_info_id = :userInfoId");
            params.put("roleIdList",roleIdList);
            params.put("userInfoId",userInfoId);

            fields.append("srp.*");
        };
        return doing.go(SysRolePermissionDto.class, jdbcTemplate, log,objectMapper);
    }
}
